package myzzyl.response;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import myzzyl.constants.ResponseCode;

@Data
@Schema(description = "基本统一响应体类")
public class BaseResponse<T> {

    @Schema(description = "响应码", example = "200")
    private Integer code;
    @Schema(description = "响应信息", example = "操作成功")
    private String msg;
    @Schema(description = "响应数据", example = "null")
    private T data;

    public BaseResponse(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public BaseResponse(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }


    /**
     * 成功不带数据
     * @return 返回值
     */
    public static <T> BaseResponse<T> success() {
        return new BaseResponse<>(ResponseCode.SUCCESS.getCode(), ResponseCode.SUCCESS.getMessage());
    }

    /**
     * 成功带数据
     * @return 返回值
     */
    public static <T> BaseResponse<T> success(T data) {
        return new BaseResponse<>(ResponseCode.SUCCESS.getCode(), ResponseCode.SUCCESS.getMessage(), data);
    }

    /**
     * 失败，消息自定义
     * @param code 响应吗
     * @param msg 响应数据
     * @return 返回值
     */
    public static <T> BaseResponse<T> error(Integer code, String msg) {
        return new BaseResponse<>(code, msg);
    }

    /**
     * 失败，消息不自定义
     * @return 返回值
     */
    public static <T> BaseResponse<T> error() {
        return new BaseResponse<>(ResponseCode.ERROR.getCode(), ResponseCode.ERROR.getMessage());
    }

    /**
     * 失败，带false返回值
     * @return 返回值
     */
    public static <T> BaseResponse<T> error(T data) {
        return new BaseResponse<>(ResponseCode.ERROR.getCode(), ResponseCode.ERROR.getMessage(), data);
    }
}
